home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / SysPal1 / SysPal1.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.1 KB  |  144 lines

  1. /*----------------------------------------
  2.    SYSPAL1.C -- Displays system palette
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. TCHAR szAppName [] = TEXT ("SysPal1") ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      HWND     hwnd ;
  16.      MSG      msg ;
  17.      WNDCLASS wndclass ;
  18.  
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  33.                       szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, TEXT ("System Palette #1"), 
  38.                           WS_OVERLAPPEDWINDOW, 
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.  
  43.      if (!hwnd)
  44.           return 0 ;
  45.  
  46.      ShowWindow (hwnd, iCmdShow) ;
  47.      UpdateWindow (hwnd) ;
  48.  
  49.      while (GetMessage (&msg, NULL, 0, 0))
  50.      {
  51.           TranslateMessage (&msg) ;
  52.           DispatchMessage (&msg) ;
  53.      }
  54.      return msg.wParam ;
  55. }
  56.  
  57. BOOL CheckDisplay (HWND hwnd)
  58. {
  59.      HDC hdc ;
  60.      int iPalSize ;
  61.  
  62.      hdc = GetDC (hwnd) ;
  63.      iPalSize = GetDeviceCaps (hdc, SIZEPALETTE) ;
  64.      ReleaseDC (hwnd, hdc) ;
  65.  
  66.      if (iPalSize != 256)
  67.      {
  68.           MessageBox (hwnd, TEXT ("This program requires that the video ")
  69.                             TEXT ("display mode have a 256-color palette."),
  70.                       szAppName, MB_ICONERROR) ;
  71.           return FALSE ;
  72.      }
  73.      return TRUE ;
  74. }
  75.  
  76. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  77. {
  78.      static int   cxClient, cyClient ;
  79.      static SIZE  sizeChar ;
  80.      HDC          hdc ;
  81.      int          i, x, y ;
  82.      PAINTSTRUCT  ps ;
  83.      PALETTEENTRY pe [256] ;
  84.      TCHAR        szBuffer [16] ;
  85.  
  86.      switch (message)
  87.      {
  88.      case WM_CREATE:
  89.           if (!CheckDisplay (hwnd))
  90.                return -1 ;
  91.  
  92.           hdc = GetDC (hwnd) ;
  93.           SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  94.           GetTextExtentPoint32 (hdc, TEXT ("FF-FF-FF"), 10, &sizeChar) ;
  95.           ReleaseDC (hwnd, hdc) ;
  96.           return 0 ;
  97.      
  98.      case WM_DISPLAYCHANGE:
  99.           if (!CheckDisplay (hwnd))
  100.                DestroyWindow (hwnd) ;
  101.  
  102.           return 0 ;
  103.  
  104.      case WM_SIZE:
  105.           cxClient = LOWORD (lParam) ;
  106.           cyClient = HIWORD (lParam) ;
  107.           return 0 ;
  108.  
  109.      case WM_PAINT:
  110.           hdc = BeginPaint (hwnd, &ps) ;
  111.  
  112.           SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  113.  
  114.           GetSystemPaletteEntries (hdc, 0, 256, pe) ;
  115.  
  116.           for (i = 0, x = 0, y = 0 ; i < 256 ; i++)
  117.           {
  118.                wsprintf (szBuffer, TEXT ("%02X-%02X-%02X"),
  119.                          pe[i].peRed, pe[i].peGreen, pe[i].peBlue) ;
  120.  
  121.                TextOut (hdc, x, y, szBuffer, lstrlen (szBuffer)) ;
  122.  
  123.                if ((x += sizeChar.cx) + sizeChar.cx > cxClient)
  124.                {
  125.                     x = 0 ;
  126.           
  127.                     if ((y += sizeChar.cy) > cyClient)
  128.                          break ;
  129.                }
  130.           }
  131.           EndPaint (hwnd, &ps) ;
  132.           return 0 ;
  133.  
  134.      case WM_PALETTECHANGED:
  135.           InvalidateRect (hwnd, NULL, FALSE) ;
  136.           return 0 ;
  137.  
  138.      case WM_DESTROY:
  139.           PostQuitMessage (0) ;
  140.           return 0 ;
  141.      }
  142.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  143. }
  144.